<script setup>
import { ref, reactive } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '../API/login-API'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const form = reactive({
    adminname: '',
    password: ''
})

const waiting = ref(false)
const formRef = ref()

const rules = reactive({
    adminname: [
        {
            // 必填
            required: true,
            // 提示语
            message: '请输入用户名',
            // 触发方式
            trigger: 'blur'
        },
        {
            //验证输入长度在2-10之间
            validator(rule, value, callback) {
                if (value.length < 3 || value.length > 10) {
                    //new Error 提示信息
                    callback(new Error('输入长度在 3-10 之间'))
                } else {
                    // 函数无参数 验证通过
                    callback()
                }
            },
            trigger: 'blur'
        }
    ],
    password: [
        {
            // 必填
            required: true,
            // 提示语
            message: '请输入密码',
            // 触发方式
            trigger: 'blur'
        },
        {
            //验证输入长度在2-10之间
            validator(rule, value, callback) {
                if (value.length < 6 || value.length > 10) {
                    //new Error 提示信息
                    callback(new Error('输入长度在 6-10 之间'))
                } else {
                    // 函数无参数 验证通过
                    callback()
                }
            },
            trigger: 'blur'
        }
    ],
})

const submitForm = async (refs) => {
    if (!refs) return
    await refs.validate(valid => {
        if (valid) {
            submint()
        }
    })

}



const submint = async () => {
    
    if(waiting.value) return

    waiting.value = true
    // 登录接口
    const result = await login(form)
    console.log(result);
    if (result.code === '200') {
        localStorage.setItem('token', result.data.token)
        localStorage.setItem('adminname',result.data.adminname)
        router.push('/main/home')
        ElMessage({
            message:result.message,
            type:'success'
        })
    }else{
        ElMessage(result.message)
    }
    
    waiting.value = false
}
</script>

<template>
    <el-form ref="formRef" class="box" :model="form" :rules="rules">
        <el-form-item>
            <label class="text">嗨&emsp;购&emsp;商&emsp;城</label>
        </el-form-item>
        <el-form-item prop="adminname">
            <el-input v-model="form.adminname" placeholder="用户名" :prefix-icon="User" />
        </el-form-item>
        <el-form-item prop="password">
            <el-input v-model="form.password" type="password" placeholder="密码" @keyup.enter="submitForm(formRef)" :prefix-icon="Lock" show-password />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm(formRef)" style="width: 100%;">登录</el-button>
        </el-form-item>
    </el-form>
</template>

<style scoped>
.box {
    width: 300px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.text {
    width: 100%;
    font-size: 40px;
    color: white;
    text-align: center;
}
</style>